<?php
session_start();
require 'lib/lib/GooglePieChart.php';

include_once("../../app/dao/database.php");

$class_db = new Database;

$query = "SELECT customer.name as customer, SUM(order_detail.total) as total FROM customer INNER JOIN corder 
        ON customer.customer_id = corder.customer INNER JOIN order_detail 
        ON corder.order_id = order_detail.order_id WHERE store=$_SESSION[id_toko] GROUP BY customer ORDER BY total DESC LIMIT 0,5";

$hasil = $class_db->select_table($query);
//print_r($hasil);

$values = array();
$customer = array();

foreach($hasil as $data){
    $values[] = $data['total'];    
    $customer[] = $data['customer'];
}

$total = 0;

for($i=0;$i<count($values);$i++){
    $total += $values[$i];
}

for($i=0;$i<count($values);$i++){
    $values[$i]= ($values[$i]/$total)*100;
}

$chart = new GooglePieChart('pc', 500, 200);
$chart->addData(new GoogleChartData(array(10,20,30)));

$data = new GoogleChartData($values);
$chart->addData($data);
		
$chart->setQueryMethod(GoogleChartApi::GET);
$data->setLabels($customer);

if ( isset($_GET['debug']) ) {
	var_dump($chart->getQuery());
	echo $chart->validate();
	echo $chart->toHtml();
}
else {
	header('Content-Type: image/png');
	echo $chart;
}
